package cn.com.homeyoda.aimate.aihealth.ui.health.viewModel;

import android.app.Application;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;

import com.wzq.mvvmsmart.binding.command.BindingAction;
import com.wzq.mvvmsmart.binding.command.BindingCommand;
import com.wzq.mvvmsmart.event.StateLiveData;
import com.wzq.mvvmsmart.utils.KLog;
import com.wzq.mvvmsmart.utils.RxUtils;
import com.wzq.mvvmsmart.utils.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import cn.com.homeyoda.aimate.aihealth.base.BaseViewModel;
import cn.com.homeyoda.aimate.aihealth.bean.BmiBean;
import cn.com.homeyoda.aimate.aihealth.bean.FamilyMembersBean;
import cn.com.homeyoda.aimate.aihealth.bean.LoginBean;
import cn.com.homeyoda.aimate.aihealth.bean.RecommendRecipeBean;
import cn.com.homeyoda.aimate.aihealth.bean.SportPlanUserBean;
import cn.com.homeyoda.aimate.aihealth.bean.SportTitleBean;
import cn.com.homeyoda.aimate.aihealth.data.Repository;
import cn.com.homeyoda.aimate.aihealth.ui.my.family.FamilyViewModel;
import cn.com.homeyoda.aimate.aihealth.views.MemberChangeDialog;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 饮食计划
 */
public class DietPlanViewMode extends BaseViewModel<Repository> {

    public StateLiveData<FamilyMembersBean.DataBean.FamilyMembers> planUserStateLiveData;
    public StateLiveData<Double> dmiStateLiveData;
    public StateLiveData<List<RecommendRecipeBean.DataBean.MenuFoodListBean>> foodStateLiveData;


    public ArrayList<FamilyMembersBean.DataBean.FamilyMembers> familyMembers;

    public DietPlanViewMode(@NonNull Application application, Repository model) {
        super(application, model);
        planUserStateLiveData = new StateLiveData<>();
        dmiStateLiveData = new StateLiveData<>();
        foodStateLiveData = new StateLiveData<>();
        familyMembers = new ArrayList<>();
    }


    public void initUserInfo() {
        if (TextUtils.isEmpty(model.getUserId())) {
            Log.d(TAG, "initUserInfo error : not login !");
            return;
        }


        model.getFamilyMembers(model.getFamilyId())
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new Observer<FamilyMembersBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        planUserStateLiveData.setLoading();
                    }

                    @Override
                    public void onNext(FamilyMembersBean response) {
                        if (response.getCode() == 200) {
                            familyMembers = response.getData().getFamilyMembers();

                            if (familyMembers.size() > 0) {
                                for (FamilyMembersBean.DataBean.FamilyMembers userMember : familyMembers) {
                                    if (model.getUserId().equals(String.valueOf(userMember.getUserId()))) {
                                        planUserStateLiveData.setValueAndSuccess(userMember);
                                    }
                                }

                                if (planUserStateLiveData.getValue() == null && familyMembers.size() > 0) {
                                    planUserStateLiveData.setValueAndSuccess(familyMembers.get(0));
                                }
                            } else {
                                planUserStateLiveData.setError();
                            }


                        } else {
                            planUserStateLiveData.setError();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        planUserStateLiveData.setError();
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                        planUserStateLiveData.setIdle();
                    }
                });
    }

    public void getFoodData() {
        String faceId = "";
        if (planUserStateLiveData.getValue() == null) {
            faceId = model.getFaceId();
        } else {
            faceId = planUserStateLiveData.getValue().getFaceId();
        }

        model.queryBmi(faceId)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new Observer<BmiBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        dmiStateLiveData.postLoading();
                    }

                    @Override
                    public void onNext(BmiBean bmiBean) {
                        if (bmiBean.getCode() == 200) {
                            dmiStateLiveData.postValueAndSuccess(bmiBean.getData());
                        } else {
                            dmiStateLiveData.postError();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dmiStateLiveData.postError();
                    }

                    @Override
                    public void onComplete() {
                        dmiStateLiveData.postIdle();
                    }
                });


        model.queryRecommendRecipe(faceId)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new Observer<RecommendRecipeBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        foodStateLiveData.postLoading();
                    }

                    @Override
                    public void onNext(RecommendRecipeBean recommendRecipeBean) {
                        if (recommendRecipeBean.getCode() == 200) {
                            foodStateLiveData.postValueAndSuccess(recommendRecipeBean.getData().getMenuFoodList());
                        } else {
                            foodStateLiveData.postError();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        foodStateLiveData.postError();
                    }

                    @Override
                    public void onComplete() {
                        foodStateLiveData.postIdle();
                    }
                });
    }


    public void changeMember(FamilyMembersBean.DataBean.FamilyMembers member) {
        if (member != null && member.getId() != planUserStateLiveData.getValue().getId()) {
            planUserStateLiveData.postValueAndSuccess(member);
            getFoodData();
        }

    }

}
